package com.riant;

import java.util.concurrent.CountDownLatch;

public class TestCountDownLatch {


    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);

        Thread thread1=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("start "+Thread.currentThread().getName());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("end "+Thread.currentThread().getName());
                countDownLatch.countDown();
            }
        });
        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("start "+Thread.currentThread().getName());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("end "+Thread.currentThread().getName());
                countDownLatch.countDown();
            }
        });
        Thread thread3=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("start "+Thread.currentThread().getName());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("end "+Thread.currentThread().getName());
                countDownLatch.countDown();
            }
        });
        thread1.start();
        thread2.start();
        thread3.start();


        System.out.println("当前任务组已经完成:"+ countDownLatch.getCount());
    }
}
